package org.example.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.order.entity.Classification;
import org.example.order.exception.ClassificationNotFoundException;
import org.example.order.mapper.ClassificationMapper;
import org.example.order.service.ClassificationService;
import org.springframework.stereotype.Service;

@Service
public class ClassificationServiceImpl implements ClassificationService {
    @Resource
    private ClassificationMapper mapper;


    @Override
    public PageInfo<Classification> getClassifications(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(mapper.selectAll());
    }

    @Override
    public Classification getClassificationById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void addClassification(Classification classification) throws RuntimeException {
        int count = mapper.insertClassification(classification);
        if (count == 0) {
            throw new RuntimeException("插入商品分类表失败");
        }
    }

    @Override
    public void updateClassification(Classification classification) throws ClassificationNotFoundException {
        int count = mapper.updateClassification(classification);
        if (count == 0) {
            throw new ClassificationNotFoundException("分类不存在，更新失败");
        }
    }

    @Override
    public void deleteClassification(Long id) {
        mapper.deleteClassification(id);
    }
}
